local skynet     = require "skynet"
local mysql      = require "skynet.db.mysql"
local cjson      = require "cjson"
local datacenter = require "skynet.datacenter"
local template   = require "template"
local redis      = require "skynet.db.redis"


require "functions"

require "skynet.manager"

local CMD = {}

local db

skynet.start(function()
    skynet.dispatch("lua", function(session, source, cmd,...)
        skynet.error("mysqld dispatch",cmd)
        local f = assert(CMD[cmd])
        skynet.ret(skynet.pack(f(...)))
    end)
    local function on_connect(db)
        db:query("set charset utf8mb4");
    end

    db=mysql.connect{
        host=MYSQL_CONF.ip,
        port=MYSQL_CONF.port,
        database=MYSQL_CONF.db,
        user=MYSQL_CONF.user,
        password=MYSQL_CONF.password,
        charset="utf8mb4",
        max_packet_size = 1024 * 1024,
        on_connect = on_connect
    }

    if not db then
        error("mysql connect fail") 
    end

    skynet.fork(function()
        while true do
            db:ping()
            skynet.sleep(300)
        end
    end)
    skynet.register "mysqld"
end)
